
#include "cPlayer.h"
#include "Animation.h"
#include "XMLHelper.h"

cPlayer::cPlayer() 
{
	mp_animations = new Animation[10];
	m_currentFrame = 0;
	GetCollider()->SetType(Collider::PLAYER);
	m_isBlinking = false;
	m_blinkCounter = 0;
}
cPlayer::~cPlayer()
{
	delete[] mp_animations;
}

void cPlayer::Draw(int tex_id)
{	
	float xo,yo,xf,yf;
	if (GetState() != STATE_HURT){
		m_isBlinking = false;
		m_blinkCounter = 0;
	}
	switch (GetState())
	{
		case STATE_HURT:
			m_isBlinking = true;
		//1
		case STATE_LOOKLEFT:
			xo = (float)mp_animations[1].GetFrame(0).first.first / 1124.0;
			yo = (float)mp_animations[1].GetFrame(0).first.second / 600.0;
			xf = (float)mp_animations[1].GetFrame(0).second.first / 1124.0;
			yf = (float)mp_animations[1].GetFrame(0).second.second / 600.0;
			break;
			//4
		case STATE_LOOKRIGHT:
			xo = (float)mp_animations[2].GetFrame(0).first.first / 1124.0;
			yo = (float)mp_animations[2].GetFrame(0).first.second / 600.0;
			xf = (float)mp_animations[2].GetFrame(0).second.first / 1124.0;
			yf = (float)mp_animations[2].GetFrame(0).second.second / 600.0;
			break;
			//1..3
		case STATE_WALKLEFT:
			xo = mp_animations[0].GetFrame(m_currentFrame).first.first / 1124.0;
			yo = mp_animations[0].GetFrame(m_currentFrame).first.second / 600.0;
			xf = mp_animations[0].GetFrame(m_currentFrame).second.first / 1124.0;
			yf = mp_animations[0].GetFrame(m_currentFrame).second.second / 600.0;
			m_currentFrame = NextFrame(10);
			break;
			//4..6
		case STATE_WALKRIGHT:
			xo = mp_animations[3].GetFrame(m_currentFrame).first.first / 1124.0;
			yo = mp_animations[3].GetFrame(m_currentFrame).first.second / 600.0;
			xf = mp_animations[3].GetFrame(m_currentFrame).second.first / 1124.0;
			yf = mp_animations[3].GetFrame(m_currentFrame).second.second / 600.0;
			m_currentFrame = NextFrame(10);
			break;
		case STATE_JUMPINGLEFT:
			xo = mp_animations[4].GetFrame(m_currentFrame).first.first / 1124.0;
			yo = mp_animations[4].GetFrame(m_currentFrame).first.second / 600.0;
			xf = mp_animations[4].GetFrame(m_currentFrame).second.first / 1124.0;
			yf = mp_animations[4].GetFrame(m_currentFrame).second.second / 600.0;
			m_currentFrame = NextFrame(8);
			break;
		case STATE_JUMPINGRIGHT:
			xo = mp_animations[5].GetFrame(m_currentFrame).first.first / 1124.0;
			yo = mp_animations[5].GetFrame(m_currentFrame).first.second / 600.0;
			xf = mp_animations[5].GetFrame(m_currentFrame).second.first / 1124.0;
			yf = mp_animations[5].GetFrame(m_currentFrame).second.second / 600.0;
			m_currentFrame = NextFrame(8);
			break;
		default:
			return;

	}

	if (m_isBlinking)
	{ 
		if(m_blinkCounter++%6 <=3)DrawRect(tex_id, xo, yo, xf, yf); 
	}
	else DrawRect(tex_id, xo, yo, xf, yf);
}

void cPlayer::LoadAnimations()
{
	Animation animation;
	int x0, y0, xf, yf;
	XMLHelper helper;
	helper.Load("Assets\\animations.xml");
	char * frame;
	frame = (char *)malloc(sizeof(char)* 30);
	strcpy(frame,"animation/walkingLeft/frame/");
	for (int i = 0; i < 11; ++i)
	{
		char * newFrame = (char *)malloc(sizeof(char)* 30);
		strcpy(newFrame, frame);
		char * frameNumber;
		frameNumber = (char *)malloc(sizeof(char)*2);
		frameNumber[0] = '1' + i;
		frameNumber[1] = '\0';
		strcat(newFrame, frameNumber);
		x0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionX0").c_str());
		y0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionY0").c_str());
		xf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionXF").c_str());
		yf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionYF").c_str());
		mp_animations[0].AddFrame(x0, y0, xf, yf);	
		free(frameNumber);
	}

	strcpy(frame, "animation/walkingRight/frame/");
	for (int i = 0; i < 10; ++i)
	{
		char * newFrame = (char *)malloc(sizeof(char)* 30);
		strcpy(newFrame, frame);
		char * frameNumber;
		frameNumber = (char *)malloc(sizeof(char)* 2);
		frameNumber[0] = '1' + i;
		frameNumber[1] = '\0';
		strcat(newFrame, frameNumber);
		x0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionX0").c_str());
		y0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionY0").c_str());
		xf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionXF").c_str());
		yf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionYF").c_str());
		mp_animations[3].AddFrame(x0, y0, xf, yf);
		free(frameNumber);
	}

	strcpy(frame, "animation/jumpLeft/frame/");
	for (int i = 0; i < 9; ++i)
	{
		char * newFrame = (char *)malloc(sizeof(char)* 30);
		strcpy(newFrame, frame);
		char * frameNumber;
		frameNumber = (char *)malloc(sizeof(char)* 2);
		frameNumber[0] = '1' + i;
		frameNumber[1] = '\0';
		strcat(newFrame, frameNumber);
		x0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionX0").c_str());
		y0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionY0").c_str());
		xf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionXF").c_str());
		yf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionYF").c_str());
		mp_animations[4].AddFrame(x0, y0, xf, yf);
		free(frameNumber);
	}
	strcpy(frame, "animation/jumpRight/frame/");
	for (int i = 0; i < 9; ++i)
	{
		char * newFrame = (char *)malloc(sizeof(char)* 30);
		strcpy(newFrame, frame);
		char * frameNumber;
		frameNumber = (char *)malloc(sizeof(char)* 2);
		frameNumber[0] = '1' + i;
		frameNumber[1] = '\0';
		strcat(newFrame, frameNumber);
		x0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionX0").c_str());
		y0 = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionY0").c_str());
		xf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionXF").c_str());
		yf = std::atoi(helper.GetElement(newFrame).GetAttributeValue("positionYF").c_str());
		mp_animations[5].AddFrame(x0, y0, xf, yf);
		free(frameNumber);
	}
	free(frame);

	x0 = std::atoi(helper.GetElement("animation/idleLeft/frame/1").GetAttributeValue("positionX0").c_str());
	y0 = std::atoi(helper.GetElement("animation/idleLeft/frame/1").GetAttributeValue("positionY0").c_str());
	xf = std::atoi(helper.GetElement("animation/idleLeft/frame/1").GetAttributeValue("positionXF").c_str());
	yf = std::atoi(helper.GetElement("animation/idleLeft/frame/1").GetAttributeValue("positionYF").c_str());
	mp_animations[1].AddFrame(x0, y0, xf, yf);

	x0 = std::atoi(helper.GetElement("animation/idleRight/frame/1").GetAttributeValue("positionX0").c_str());
	y0 = std::atoi(helper.GetElement("animation/idleRight/frame/1").GetAttributeValue("positionY0").c_str());
	xf = std::atoi(helper.GetElement("animation/idleRight/frame/1").GetAttributeValue("positionXF").c_str());
	yf = std::atoi(helper.GetElement("animation/idleRight/frame/1").GetAttributeValue("positionYF").c_str());
	mp_animations[2].AddFrame(x0, y0, xf, yf);
}
